//
//  CRMViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/8/1.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "CRMViewController.h"
#import "RHTableView.h"
#import "LicensePlateTableViewCell.h"
#import "CRMTableViewCell.h"
#import "SelectRightTypeView.h"
#import "V4AddLicensePlateViewController.h"
#import "AddUserViewController.h"

@interface CRMViewController ()<SelectRightTypeViewDelegate>{
    UIScrollView *viewTopBG;
    UIView *viewLine;
    NSArray *arrayType;
    
    RHTableView *tableLeft;
    RHTableView *tableConter;
    RHTableView *tableRight;
    float temp_height;
    
    BOOL boolLoadConter;
    BOOL boolLoadRight;
    
    NSMutableArray *arrayTypeRight;
    SelectRightTypeView *selctTypeView;
    BOOL boolRefresh;
}

@end

@implementation CRMViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (boolRefresh) {
        boolRefresh=NO;
        if (tableLeft.hidden==NO) {
            
            [self loadLeftTableData];
        }else if (tableConter.hidden==NO){
            
            [self loadConterTableData];
        }else{
            
            [self loadRightTableData];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self rightButton:nil image:@"jia" sel:@selector(rightButtonClicked)];
    arrayTypeRight=[NSMutableArray new];
    [arrayTypeRight addObject:@{@"title":@"客户录入"}];
    [arrayTypeRight addObject:@{@"title":@"销售单录入"}];
    [arrayTypeRight addObject:@{@"title":@"牌照录入"}];
    
    
    [self initTabelViews];
    [self initTopButtonClicked];
    
}

-(void)initTopButtonClicked{
    viewTopBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, 40)];
    viewTopBG.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:viewTopBG];
    [self.view addSubview:[RHMethods lineViewWithFrame:CGRectMake(0, YH(viewTopBG)-0.5, kScreenWidth, 0.5)]];
    
    viewLine=[RHMethods lineViewWithFrame:CGRectMake(0, H(viewTopBG)-2, 10, 2)];
    [viewLine setBackgroundColor:rgbpublicColor];
    [viewTopBG addSubview:viewLine];
    arrayType=@[@{@"name":@"客户",@"id":@"1"},
                @{@"name":@"销售单",@"id":@"2"},
                @{@"name":@"牌照",@"id":@"3"}];
    
    float fjg=10;
    float fx=fjg;
    float fw=(kScreenWidth-20)/3;
    for (int i=0;i<[arrayType count]; i++) {
        NSString *strTitle=[[arrayType objectAtIndex:i] valueForJSONStrKey:@"name"];
        UIButton *btnT=[RHMethods buttonWithFrame:CGRectMake(fx, 0, fw, H(viewTopBG)) title:strTitle image:nil bgimage:nil];
        [viewTopBG addSubview:btnT];
        btnT.tag=10+i;
        [btnT setTitleColor:rgbpublicColor forState:UIControlStateSelected];
        [btnT addTarget:self action:@selector(typeSelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        fx+=fw;
        if (i==0) {
            [self typeSelectButtonClicked:btnT];
        }
    }
    [viewTopBG setContentSize:CGSizeMake(kScreenWidth, 39)];
}

-(void)initTabelViews{
    
    tableLeft=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight+40, kScreenWidth, kContentHeight-40)];
    [tableLeft showRefresh:YES LoadMore:YES];
    tableLeft.delegate = self;
    tableLeft.dataSource = self;
    [tableLeft setBackgroundColor:rgbGray];
    [tableLeft setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:tableLeft];
    [tableLeft setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    
    tableConter=[[RHTableView alloc]initWithFrame:tableLeft.frame];
    [tableConter showRefresh:YES LoadMore:YES];
    tableConter.delegate = self;
    tableConter.dataSource = self;
    [tableConter setBackgroundColor:rgbGray];
    [tableConter setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:tableConter];
    [tableConter setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    tableConter.hidden=YES;

    
    tableRight=[[RHTableView alloc]initWithFrame:tableLeft.frame];
    [tableRight showRefresh:YES LoadMore:YES];
    tableRight.delegate = self;
    tableRight.dataSource = self;
    [tableRight setBackgroundColor:rgbGray];
    [tableRight setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:tableRight];
//    [tableRight registerClass:[LicensePlateTableViewCell class] forCellReuseIdentifier:@"LicensePlateTableViewCell"];
    [tableRight setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    tableRight.hidden=YES;
}

-(void)loadLeftTableData{
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    tableLeft.urlString=[NSString stringWithFormat:@"%@%@",XKFV4customer,dictparam.wgetParamStr];
    [tableLeft refresh];
}
-(void)loadConterTableData{
    boolLoadConter=YES;
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    tableConter.urlString=[NSString stringWithFormat:@"%@%@",XKFV4customerslip,dictparam.wgetParamStr];
    [tableConter refresh];
}
-(void)loadRightTableData{
    boolLoadRight=YES;
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    [dictparam setValue:@"crm" forKey:@"type"];
    tableRight.urlString=[NSString stringWithFormat:@"%@%@",XKFV4productslists,dictparam.wgetParamStr];
    [tableRight refresh];
}
#pragma mark button
-(void)typeSelectButtonClicked:(UIButton *)btn{
    for (UIView *view in [viewTopBG subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btnT=(UIButton *)view;
            btnT.selected=NO;
        }
    }
    NSString * strType_cid=[[arrayType objectAtIndex:btn.tag-10] valueForJSONStrKey:@"id"];
    if ([strType_cid isEqualToString:@"1"]) {
        tableLeft.hidden=NO;
        tableConter.hidden=YES;
        tableRight.hidden=YES;
        [self loadLeftTableData];
    }else if ([strType_cid isEqualToString:@"2"]){
        tableLeft.hidden=YES;
        tableConter.hidden=NO;
        tableRight.hidden=YES;
//        if (!boolLoadConter) {
            [self loadConterTableData];
//        }
    }else{
        tableLeft.hidden=YES;
        tableConter.hidden=YES;
        tableRight.hidden=NO;
//        if (!boolLoadRight) {
            [self loadRightTableData];
//        }
    }
    btn.selected=YES;
    viewLine.frameWidth=btn.frameWidth;
    [UIView animateWithDuration:0.3 animations:^{
        viewLine.frameX=X(btn);
    }];
    
    
}
-(void)rightButtonClicked{    
    if (!selctTypeView) {
        selctTypeView=[[SelectRightTypeView alloc] init];
        selctTypeView.dicType=@{@"list":arrayTypeRight};
        selctTypeView.contentConter=YES;
    }
    selctTypeView.delegate=self;
    [selctTypeView show];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==tableLeft) {
        return tableLeft.dataArray.count;
    }else if (tableView==tableConter){
        return tableConter.dataArray.count;
    }else{
        return tableRight.dataArray.count;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==tableLeft || tableView==tableConter) {
        [self tableView:tableView cellForRowAtIndexPath:indexPath];
        return temp_height;
    }else{
        [self tableView:tableView cellForRowAtIndexPath:indexPath];
        return temp_height;
//        NSDictionary *dic=[tableRight.dataArray objectAtIndex:indexPath.row];
//            float fth=[self heightForLabel:kScreenWidth-50 font:fontTitle text:[dic valueForJSONStrKey:@"stitle"]];
//            fth = fth>20?40:20;
//            return fth+65;
////        return 100;
    }
//
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==tableLeft) {
        NSDictionary *dic=[tableLeft.dataArray objectAtIndex:indexPath.row];
        static NSString * strIdentifier=@"CRMTableViewCell";
        CRMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
        if (!cell) {
            cell = [[CRMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdentifier];
        }
        temp_height=[cell setValueForDictionary:dic indexPath:indexPath type:@"crm"];
        return cell;
    }else if (tableView==tableConter){
        NSDictionary *dic=[tableConter.dataArray objectAtIndex:indexPath.row];
        static NSString * strIdentifier=@"CRMTableViewCell_conter";
        CRMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
        if (!cell) {
            cell = [[CRMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdentifier];
        }
        temp_height=[cell setValueForDictionary:dic indexPath:indexPath type:@"crm"];
        return cell;
    }else{
        NSDictionary *dic=[tableRight.dataArray objectAtIndex:indexPath.row];
        static NSString * strIdentifier=@"LicensePlateTableViewCell";
        LicensePlateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
        if (!cell) {
            cell = [[LicensePlateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdentifier];
        }
        temp_height=[cell setValueForDictionary:dic indexPath:indexPath type:@"crm" searchKey:@""];
        return cell;
        
//        NSDictionary *dic=[tableRight.dataArray objectAtIndex:indexPath.row];
//        LicensePlateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LicensePlateTableViewCell" forIndexPath:indexPath];
//        [cell setValueForDictionary:dic indexPath:indexPath type:@"crm"];
//        return cell;
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView==tableLeft) {
        NSDictionary *dic=[tableLeft.dataArray objectAtIndex:indexPath.row];
        krequestParam
        [dictparam setValue:[dic valueForJSONStrKey:@"id"] forKey:@"id"];
        [NetEngine createPostAction:XKFV4customercustomer_detail withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                [self pushController:[AddUserViewController class] withInfo:@"userEdit" withTitle:@"编辑客户" withOther:[[resData objectForJSONKey:@"data"] objectForJSONKey:@"detail"]];
            }else{
                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            }
        }];
        
        
    }else if (tableView==tableConter){
        NSDictionary *dic=[tableConter.dataArray objectAtIndex:indexPath.row];
        krequestParam
        [dictparam setValue:[dic valueForJSONStrKey:@"id"] forKey:@"id"];
        [NetEngine createPostAction:XKFV4customerslip_detail withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                [self pushController:[AddUserViewController class] withInfo:@"sellEdit" withTitle:@"编辑销售单" withOther:[[resData objectForJSONKey:@"data"] objectForJSONKey:@"detail"]];
            }else{
                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            }
        }];

        
    }else{
        NSDictionary *dic=[tableRight.dataArray objectAtIndex:indexPath.row];
        krequestParam
        [dictparam setValue:[dic valueForJSONStrKey:@"id"] forKey:@"id"];
        [NetEngine createPostAction:XKFV4productsdetails withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                [self pushController:[V4AddLicensePlateViewController class] withInfo:@"Edit" withTitle:@"编辑牌照" withOther:[resData objectForJSONKey:@"data"]];
            }else{
                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            }
        }];
        

    }
}


#pragma mark SelectRightTypeViewDelegate
-(void)selectRightTypeView:(SelectRightTypeView *)sview object:(NSDictionary *)dic index:(NSIndexPath *)indexPath{
    /*
     [arrayTypeRight addObject:@{@"title":@"客户录入"}];
     [arrayTypeRight addObject:@{@"title":@"销售单录入"}];
     [arrayTypeRight addObject:@{@"title":@"牌照录入"}];
     */
    if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"客户录入"]){
        [self pushController:[AddUserViewController class] withInfo:@"user" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"销售单录入"]){
        [self pushController:[AddUserViewController class] withInfo:@"sell" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"牌照录入"]){
        [self pushController:[V4AddLicensePlateViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
    }
}
#pragma mark pop
-(void)popRefreshData{
    boolRefresh=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
